
/* GCSx
** DATATYPE.CPP
**
** Simple data-type conversion
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

const string blankString("");
int vPtrSize = 1;
int bcFloatSize = 1;

// Buffer for formatString and float/intToStr
char fsBuffer[256];

const char* intToStr(long value) {
    snprintf(fsBuffer, 256, "%ld", value);
    return fsBuffer;
}

const char* floatToStr(BCfloat value) {
    snprintf(fsBuffer, 256, "%"BC_FLOAT_PRINTF, value);
    return fsBuffer;
}

long strToInt(const string& value) {
    return strtol(value.c_str(), NULL, 10);
}

long strToIntErr(const string& value) throw_int {
    const char* src = value.c_str();
    char* end = NULL;
    long result = strtol(src, &end, 10);
    if (src == end) throw 2;
    if (*end != 0) throw 1;
    return result;
}

BCfloat strToFloat(const string& value) {
    return BC_FLOAT_CONVERT(value.c_str(), NULL);
}

string formatString(const char* format, ...) {
    string value;

    assert(format);

    va_list arglist;
    va_start(arglist, format);

    vsnprintf(fsBuffer, 256, format, arglist);
    value = fsBuffer;

    va_end(arglist);
    return value;
}

void matrixCopy(const void* source, void* dest, int w, int h, int sourcePitch, int destPitch) {
    assert(source);
    assert(dest);
    assert(w <= sourcePitch);
    assert(w <= destPitch);
    assert(w >= 0);
    assert(h >= 0);
    
    if ((w == 0) || (h == 0)) return;
    
    // Optimized form
    if ((w == sourcePitch) && (w == destPitch)) {
        memcpy(dest, source, w * h);
    }
    else {
        for (; h > 0; --h) {
            memcpy(dest, source, w);
            source = (const char*)source + sourcePitch;
            dest = (char*)dest + destPitch;
        }
    }
}

void memSet32(Uint32* dest, Uint32 value, int size) {
    assert(dest);
    
    for (; size > 0; --size) {
        *dest++ = value;
    }
}

int myStricmp(const char* a, const char* b) {
    while ((*a) && (*b) && (tolower(*a) == tolower(*b))) {
        ++a;
        ++b;
    }
    return (int)tolower(*a) - (int)tolower(*b);
}

void toLower(string& str) {
    for (int pos = str.size() - 1; pos >= 0; --pos) {
        str[pos] = tolower(str[pos]);
    }
}

char* newCpCopy(const string& fromStr) {
    char* str = new char[fromStr.size() + 1];
    strcpy(str, fromStr.c_str());
    return str;
}

